old_posts/2017-9-25-Develop Azure Functions on any platform.html (90 lines of code) (raw):
---
layout: post
hide_excerpt: true
---
<html><head>
<meta charset="utf-8"/>
</head>
<body>
<div id="page">
<a class="url fn n profile-usercard-hover" href="https://social.msdn.microsoft.com/profile/Donna Malayeri" target="_blank">Donna Malayeri</a>
<time> 9/25/2017 6:00:20 AM</time>
<hr/>
<div id="content">I’m excited to announce that we have ported Azure Functions to .NET Core 2.0! Both the runtime and the Azure Functions Core Tools are now cross-platform. Now, you can debug C# and JavaScript functions on a Mac or Linux using the Core Tools and Visual Studio Code. Both the runtime and the Core Tools are still in preview and we welcome your feedback!
As this is a preview release, there are still a number of feature gaps. For more information, see <a href="https://github.com/Azure/azure-webjobs-sdk-script/wiki/Azure-Functions-runtime-2.0-known-issues">Azure Functions runtime 2.0 known issues</a>.
<h2>Running on your local machine</h2>
To get the new version of the core tools, pull down the <strong>@core</strong> tag on npm:
<pre>npm i -g azure-functions-core-tools@core</pre>
If you’re using Ubuntu, prefix the command above with "sudo."
If you have problems with the npm install on Mac, use the following:
<pre>sudo npm i -g azure-functions-core-tools@core --unsafe-perm</pre>
To learn how to use the tools, see <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local">Code and test Azure functions locally</a>.
<h3>JavaScript (Node 8.5 or higher)</h3>
For the easiest installation, you must be running <strong>Node version 8.5 or higher</strong>. See instructions below for how to target a lower version.
To create a new JavaScript HTTP-triggered function, do the following:
<pre>mkdir JavaScriptHttp
cd JavaScriptHttp
func init .
func new --language JavaScript --template HttpTrigger --name HttpTriggerJavaScript</pre>
Run the host. This automatically enables debugging with the Node port 5858:
<pre>func host start</pre>
<a href="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-22-at-2.48.45-PM.png"><img alt="" class="alignnone size-large wp-image-5945" height="657" src="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-22-at-2.48.45-PM-1024x765.png" width="879"/></a>
Open the folder in Visual Studio Code: <code>code .</code>
In VSCode, set a breakpoint at the first line of the function, and attach the debugger (via F5 or the debug toolbar). Then, in a browser, navigate to the URL <code>http://localhost:7071/api/HttpTriggerJavaScript?name=Functions%20Everywhere!</code>
You’ll then see the breakpoint being hit in VSCode!
<a href="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-22-at-2.55.17-PM.png"><img alt="" class="alignnone size-large wp-image-5955" height="467" src="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-22-at-2.55.17-PM-1024x544.png" width="879"/></a>
<a href="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-24-at-6.30.12-PM.png"><img alt="" class="alignnone size-mediumlarge wp-image-5956" height="307" src="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-24-at-6.30.12-PM-500x307.png" width="500"/></a>
<h3>JavaScript (Node versions prior to 8)</h3>
After installing azure-functions-core-tools, run the following commands:
<pre>npm i -g node-pre-gyp
cd %userprofile%/.azurefunctions/bin/workers/node/grpc
node-pre-gyp install
</pre>
Once these tools are installed, you can use the instructions in the previous section to run and debug JavaScript functions.
<h3>C# .NET Standard 2.0 class library</h3>
You can now run and debug C# functions on a Mac or Linux. The <a href="https://www.nuget.org/packages/Microsoft.NET.Sdk.Functions">Microsoft.NET.Sdk.Functions</a> is the package that identifies a project as Functions project to Visual Studio and generates function.json from attributes during build. Templates for C# class libraries aren’t yet available in the Core Tools, but you can get a <a href="https://github.com/lindydonna/CSharpHttpCore">sample from GitHub</a>.
<h4>Dotnet command line</h4>
<pre>git clone https://github.com/lindydonna/CSharpHttpCore.git
cd CSharpHttpCore
dotnet build
dotnet publish
cd HttpTriggerCore/bin/Debug/netstandard2.0
func host start
</pre>
<h4>VS Code debugging</h4>
To debug your C# functions, open the folder containing your .csproj in VS Code. Make sure you have installed the C# extension.
<ul>
<li>In the debug toolbar next to the play button, select Add Configuration</li>
<li>Select <strong>.NET Core</strong> as the environment, then <strong>.NET: Attach to local .NET Core Console App</strong>.</li>
</ul>
This will generate a launch.json configuration for your project. Then, press F5 and select <strong>.NET Core Attach</strong>. Select the <strong>dotnet</strong> process with the command line <strong>Azure.Functions.Cli.dll host start</strong>.
Browse to the URL <code>http://localhost:7071/api/HttpTriggerCSharp?name=CSharpEverywhere!</code>. You’ll then see your breakpoint hit in VSCode.
<a href="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-24-at-11.08.42-PM1.png"><img alt="" class="alignnone size-large wp-image-5975" height="414" src="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-09-24-at-11.08.42-PM1-1024x482.png" width="879"/></a>
<h4>Visual Studio</h4>
First, ensure you have downloaded the @core version of azure-functions-core-tools: <code>npm i -g azure-functions-core-tools@core</code>
Then, add a new launch configuration for the 2.0 version of the Core Tools:
<ul>
<li>In project properties -> Debug, change <strong>Launch</strong> to <strong>Executable</strong></li>
<li>For Executable, use <strong>%APPDATA%\npm\func.cmd</strong></li>
<li>For Application Arguments, use <strong>host start</strong></li>
<li>For working directory, use <strong>$(TargetDir)</strong></li>
</ul>
F5 will now launch the new version of the Azure Functions Core Tools.
<a href="{{ site.baseurl }}/media/2017/09/vs.png"><img alt="" class="alignnone size-large wp-image-5985" height="541" src="{{ site.baseurl }}/media/2017/09/vs-1024x630.png" width="879"/></a>
<h2>Running Functions 2.0 in Azure</h2>
You can also use the .NET Core 2.0 port in Azure by targeting the new Functions 2.0 preview runtime. To use it, select "beta" in <strong>Function app settings -> Runtime version</strong>. Alternatively, you can the app setting <code>FUNCTIONS_EXTENSION_VERSION</code> to the value <code>beta</code>. You will then see a different set of templates available in the Add New Function page.
<a href="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-10-05-at-9.39.28-PM.png"><img alt="" class="alignnone size-mediumlarge wp-image-6156" height="211" src="{{ site.baseurl }}/media/2017/09/Screen-Shot-2017-10-05-at-9.39.28-PM-500x211.png" width="500"/></a>
Since the 2.0 runtime is in preview, there may be breaking changes even in minor releases. So, the 2.0 runtime should <strong>not</strong> be used for production workloads.
If you navigate to the root of you function app, you’ll see that you’re running the new version:
<a href="{{ site.baseurl }}/media/2017/09/functions2.0.png"><img alt="" class="alignnone size-medium wp-image-5987" height="210" src="{{ site.baseurl }}/media/2017/09/functions2.0-300x210.png" width="300"/></a>
<h2>Connect with us</h2>
We've seen a lot of excitement and interest, so we're looking forward to getting your feedback as we finalize the Functions 2.0 runtime.
<ul>
<li>To report bugs or file feature requests, please open an issue on the <a href="https://github.com/Azure/Azure-Functions">Azure-Functions</a> GitHub repo.</li>
<li>For technical questions, please post on the <a href="https://social.msdn.microsoft.com/Forums/azure/en-US/home?forum=azurefunctions">MSDN forums</a> or <a href="https://stackoverflow.com/questions/tagged/azure-functions">StackOverflow</a>. The entire Functions engineering team monitors these questions, so you’re sure to get an expert answer.</li>
<li>For product news, follow <a href="https://twitter.com/AzureFunctions">@AzureFunctions</a>.</li>
</ul></div>
</div></body>
<script src="{{ site.baseurl }}/resource/jquery-1.12.1.min.js" type="text/javascript"></script>
<script src="{{ site.baseurl }}/resource/replace.js" type="text/javascript"></script>
</html>